//
//  SKRouter.m
//  WeChat
//
//  Created by 陆宁 on 2017/11/6.
//  Copyright © 2017年 LN. All rights reserved.
//

#import "LNRouter.h"

@interface LNRouter()

/// viewModel到viewController的映射
@property (nonatomic,copy) NSDictionary *viewModelViewMappings;
@end

@implementation LNRouter

static LNRouter *sharedInstance_ = nil;

+(id)allocWithZone:(struct _NSZone *)zone{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [super allocWithZone:zone];
    });
    
    return sharedInstance_;
}

- (id)copyWithZone:(NSZone *)zone
{
    return sharedInstance_;
}


+(instancetype)sharedInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance_ = [[self alloc] init];
    });
    
    return sharedInstance_;
}

-(LNViewController *)viewContollerForViewModel:(LNViewModel *)viewModel{
    
    NSString *viewController = self.viewModelViewMappings[NSStringFromClass(viewModel.class)];
    
    NSParameterAssert([NSClassFromString(viewController) isSubclassOfClass:[LNViewController class]]);
    NSParameterAssert([NSClassFromString(viewController) instancesRespondToSelector:@selector(initWithViewModel:)]);
    
    return [[NSClassFromString(viewController) alloc] initWithViewModel:viewModel];
}

/// 这里是viewModel -> ViewController的映射
/// If You Use Push 、 Present 、 ResetRootViewController ,You Must Config This Dict
- (NSDictionary *)viewModelViewMappings {
    
    return @{
              @"LNMainViewModel":@"LNMainController" // 主框架
              ,@"LNHomeViewModel":@"LNHomeViewController" // 首页
              ,@"LNShopCartViewModel":@"LNShopCartViewController" // 购物车
              ,@"LNMineViewModel":@"LNMineViewController" // 我的
              ,@"LNLoginViewModel":@"LNLoginController" // 登录页
              };
}
@end

